package com.stp.oversold.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.stp.oversold.entity.SecondKill;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface SecondKillMapper extends BaseMapper<SecondKill> {
    /**
     * 将此行数据进行加锁，当整个方法将事务提交后，才会解锁
     *
     * @param skgId
     * @return
     */
    @Select(value = "SELECT * FROM second_kill WHERE second_kill_id=#{skgId} FOR UPDATE")
    SecondKill querySecondKillForUpdate(@Param("skgId") Long skgId);


    @Update(value = "UPDATE second_kill SET number = number-1 WHERE second_kill_id=#{skgId} AND number > 0")
    int updateSecondKillById(@Param("skgId") long skgId);
}
